<?php

/*
 * Fresns Control Panel (Standalone)
 * Copyright (C) 2021-Present 刘鑫
 * Released under the MIT License.
 */

namespace App\Plugins\FcpApi\TweetLanguages;

use App\Base\Services\BaseAdminService;
use App\Http\FresnsApi\Helpers\ApiConfigHelper;
use App\Plugins\FcpApi\TweetConfigs\TweetConfigsConfig;
use App\Plugins\FcpApi\TweetConfigs\TweetConfigService;

class FsService extends BaseAdminService
{
    public function __construct()
    {
        $this->model = new FsModel();
        $this->resource = FsResource::class;
        $this->resourceDetail = FsResourceDetail::class;
    }

    public function common()
    {
        $common = parent::common();

        return $common;
    }

    //获取对应的多语言
    public static function getLanguageByTableId($table, $field, $tableId, $langTag)
    {
        $lang_content = TweetLanguages::where('table_name', $table)->where('table_field', $field)->where('table_id', $tableId)->where('lang_tag', $langTag)->value('lang_content');

        return $lang_content;
    }

    public static function getLanguageByConfigs($table, $field, $tableKey, $langTag)
    {
        $lang_content = TweetLanguages::where('table_name', $table)->where('table_field', $field)->where('table_key', $tableKey)->where('lang_tag', $langTag)->value('lang_content');

        return $lang_content;
    }

    //插入到表数据
    public static function addLanguages($json, $tableName, $tableField, $tableId)
    {
        FsModel::where('table_name', $tableName)->where('table_field', $tableField)->where('table_id', $tableId)->delete();
        $langArr = json_decode($json, true);
        $itemArr = [];
        foreach ($langArr as $lang) {
            $item = [];
            $item['table_name'] = $tableName;
            $item['table_field'] = $tableField;
            $item['table_id'] = $tableId;
            $item['lang_tag'] = $lang['key'];
            $item['lang_content'] = $lang['lang_content'] ?? null;
            $itemArr[] = $item;
        }
        FsModel::insert($itemArr);
    }

    //获取表数据
    public static function getLanguages($tableName, $tableField, $tableId)
    {
        $languageArr = TweetConfigService::getLanguageStatus();
        $languagesOption = $languageArr['languagesOption'];
        //是否开启多语言
        $languageStatus = ApiConfigHelper::getConfigByItemKey('language_status');
        //默认语言
        $defaultLanguage = ApiConfigHelper::getConfigByItemKey(TweetConfigsConfig::DEFAULT_LANGUAGE);
        //查询对应的语言
        $langMap = TweetLanguages::where('table_name', $tableName)
        ->where('table_field', $tableField)
        ->where('table_id', $tableId)
        ->pluck('lang_content', 'lang_tag')
        ->toArray();

        $languageArr = [];
        if ($langMap) {
            if($languageStatus === false){
                foreach ($languagesOption as $languages) {
                    $it = [];
                    if($languages['key'] == $defaultLanguage){
                        $it['key'] = $languages['key'];
                        $it['text'] = $languages['text'];
                        $it['lang_content'] = $langMap[$languages['key']] ?? '';
                        $languageArr[] = $it;
                    }
                }
            } else {
                foreach ($languagesOption as $languages) {
                    $it = [];
                    $it['key'] = $languages['key'];
                    $it['text'] = $languages['text'];
                    $it['lang_content'] = $langMap[$languages['key']] ?? '';
                    $languageArr[] = $it;
                }
            }
            
        }

        return $languageArr;
    }
}
